package javablackbelt.beans;

import java.text.SimpleDateFormat;
import java.util.Locale;

import javablackbelt.i18n.LocaleHolder;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;

public class SimpleAccountReportGenerator implements AccountReportGenerator, MessageSourceAware {
	
	private MessageSource messageSource;
	
	/* (non-Javadoc)
	 * @see javablackbelt.beans.factory.AccountReportGenerator#generateReport(javablackbelt.beans.BankService, javablackbelt.beans.CustomerAccount)
	 */
	public String generateReport(BankService bankService, CustomerAccount account) {
		StringBuilder reportBuilder = new StringBuilder();
		
		double currentBalance = bankService.computeBalanceWithInterests(account.getAverageBalance());
		SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy");
		
		Locale userLocale = LocaleHolder.getLocale();
		
		reportBuilder.append("\n" +
				this.messageSource.getMessage("L_ACCOUNT_REPORT", null, userLocale) + "\n" +
				this.messageSource.getMessage("L_ACCOUNT_NUMBER", 
											  new Object[] {account.getAccountNumber()}, 
											  userLocale) + "\n" +
				this.messageSource.getMessage("L_ACCOUNT_BALANCE", 
											  new Object[] {currentBalance}, 
											  userLocale) + "\n" +
				this.messageSource.getMessage("L_ACCOUNT_OPEN_DATE", 
											  new Object[] {sdf.format(account.getOpenDate())}, 
											  userLocale));
		return reportBuilder.toString();
	}

	public MessageSource getMessageSource() {
		return messageSource;
	}

	@Required
	public void setMessageSource(MessageSource messageSource) {
		this.messageSource = messageSource;
	}
	
}
